package com.fdc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdc_project.entity.Customer;
import com.fdc_project.entity.Productinformation;
import com.fdc_project.mapper.CustomerMapper;
import com.fdc_project.mapper.ProductinformationMapper;
import com.fdc_project.service.IProductinformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdc_project.vo.ErrorCode;
import com.fdc_project.vo.Result;
import com.sun.applet2.preloader.event.ErrorEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sk
 * @since 2021-11-29
 */
@Service
public class ProductinformationServiceImpl extends ServiceImpl<ProductinformationMapper, Productinformation> implements IProductinformationService {

    @Autowired
    ProductinformationMapper productinformationMapper;
    @Autowired
    CustomerServiceImpl customerService;

    @Override
    public Result add(Productinformation productinformation) {
        //查询有无该客户信息
        if(customerService.selectone(productinformation.getCusId()).getData() == null) {
            return Result.fail(ErrorCode.USER_NULL.getCode(), "客户不存在！");
        }
        if (productinformationMapper.insert(productinformation) > 0) {
            System.out.println("添加成功");
            return Result.success("添加成功");
        }
        return Result.fail(-10001, "添加失败");
    }

    @Override
    public Result delete(Integer id) {
        Productinformation productinformation = productinformationMapper.selectById(id);
        if(productinformation == null || productinformation.getDeleted() == 1){
            return  Result.fail(ErrorCode.USER_NULL.getCode(),"项目不存在!");
        }
        productinformation.setDeleted(1);
        if(productinformationMapper.updateById(productinformation)>0){
            return Result.success("删除成功！");
        }
        else{
            return Result.fail(-300,"删除失败");
        }
    }

    @Override
    public Result update(Productinformation productinformation) {
        Productinformation productinformation1 = productinformationMapper.selectById(productinformation);
        if (productinformation1 == null || productinformation1.getDeleted() == 1) {
            return Result.fail(ErrorCode.USER_NULL.getCode(), "项目信息不存在！");
        }
        if (productinformationMapper.updateById(productinformation)> 0) {
            return Result.success(productinformation);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "更新不成功！");
    }

    @Override
    public Result seletone(Integer id) {
        Productinformation productinformation = productinformationMapper.selectById(id);
        if(productinformation == null || productinformation.getDeleted() == 1){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "不存在该项目");
        }
        return Result.success(productinformation);
    }

    @Override
    public Result seletcall() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted",0);
        List<Productinformation> productinformations = productinformationMapper.selectList(queryWrapper);
        if (productinformations.size() > 0) {
            return Result.success(productinformations);
        }
        return Result.fail(ErrorCode.USER_NULL.getCode(), "当前未含有任何项目！");
    }

}
